
param ( [array]$contractFiles, [string]$outputDir )

trap { break; }

$request = [xml]@"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <GenerateImages xmlns="http://cz.o2.com/ContractDocumentation">
      <ContractFiles>
		<template/>
      </ContractFiles>
    </GenerateImages>
  </soap:Body>
</soap:Envelope>
"@

$files = ""
foreach ($contractFile in $contractFiles)
{
	$schema = [Convert]::ToBase64String([IO.File]::ReadAllBytes((resolve-path $contractFile)))
	$files += "<SerializedFile><Name>$contractFile</Name><Content>$schema</Content></SerializedFile>"
}
$request.Envelope.Body.GenerateImages.ContractFiles.set_InnerXml($files)

$headers = @{}
$headers["Content-Type"] = "text/xml"
$headers["SOAPAction"] = "http://cz.o2.com/ContractDocumentation/ContractDocumentationService/GenerateImages"

$request = $request.get_OuterXml()
$response = post-uri "http://localhost:8080/ContractDocumentationService" $request -headers $headers

$xml = [xml]$response
$files = $xml.Envelope.Body.GenerateImagesResponse.GenerateImagesResult.SerializedFile
new-item $outputDir -type directory -force | out-null
if ($files)
{
	foreach ($file in $files)
	{
		write-host $file.Name
		[IO.File]::WriteAllBytes((join-path (resolve-path $outputDir) $file.Name), [Convert]::FromBase64String($file.Content))
	}
}
